package com.digital.herbarium.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import com.digital.herbarium.Entity.Atlas;
import com.digital.herbarium.Entity.Stat;
import com.digital.herbarium.Exception.CustomException;
import com.digital.herbarium.Mapper.AtlasMapper;
import com.digital.herbarium.Util.ExcelUtilHandler;
import com.digital.herbarium.Util.TimeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class AtlasService {

    @Autowired
    private AtlasMapper atlasMapperr;


    public PageInfo<Atlas> getAtlasInfo (Atlas atlas, int pageSize, int pageNum){
        PageHelper.startPage(pageNum, pageSize);
        List<Atlas> ilist =  atlasMapperr.getAtlasInfo(atlas);
        PageInfo<Atlas> pageInfo = new PageInfo<>(ilist);
        return  pageInfo;

    }

    public HashMap<String, Object> createList(Atlas atlas, int pSize, int pNum){
        HashMap<String, Object> map = new HashMap<>();
        int currPage = (pNum - 1) * pSize;
        atlas.setpNum(currPage);
        atlas.setpSize(pSize);
        List<Atlas> list = atlasMapperr.getTabList(atlas);
        List<Atlas> atlasInfo = atlasMapperr.getAtlasInfo(null);
        List<Atlas> tlist = atlasMapperr.getAllType();
        list.stream().forEach(al -> {
            List<Atlas> atlasList = getAtlas(al, atlasInfo);
            // 添加二级分类
            List<Atlas>  aslist = new ArrayList<>();
            tlist.stream().forEach(tl ->{
                Atlas as = new Atlas();
                as.setAtlasType(tl.getAtlasType());
                as.setTabList(getAtlasType(tl, atlasList));
                aslist.add(as);
            });

            al.setTabList(aslist);
        });
        map.put("list",list);
        map.put("total",atlasMapperr.getTabTotal());
        return map;
    }

    public List<Atlas> getAtlasType(Atlas atlas, List<Atlas> alist){
        List<Atlas> tl = new ArrayList<>();
        alist.stream().forEach(at ->{
            if(at.getAtlasType() != null && atlas.getAtlasType() != null){
                if(at.getAtlasType().equals(atlas.getAtlasType())){
                    tl.add(at);
                }
            }


        });
        return  tl;
    }


    public List<Atlas> getAtlas(Atlas atlas, List<Atlas> alist){
        List<Atlas> al = new ArrayList<>();
        alist.stream().forEach(at ->{
            if(at.getMedicineName() != null && atlas.getMedicineName() != null &&
                    at.getOriginalPlant() != null && atlas.getOriginalPlant() != null
            && at.getLatinName() != null &&  atlas.getLatinName() != null){
                if(at.getMedicineName().equals(atlas.getMedicineName()) &&
                        at.getOriginalPlant().equals(atlas.getOriginalPlant())
                        && at.getLatinName().equals(atlas.getLatinName())){
                    al.add(at);
                }
            }


        });
        return  al;
    }


    public void addAtlasInfo(Atlas atlas){
        check(atlas.getSampleNumber());
        atlas.setState(1);
        atlas.setCreatetime(TimeUtil.getNow());
        atlasMapperr.addAtlasInfo(atlas);

    }

    public void check(String sampleNumber){
        Atlas d = new Atlas();
        d.setSampleNumber(sampleNumber);
        List<Atlas> dlist = atlasMapperr.getAtlasInfoByNumber(d);
        if(dlist != null && dlist.size() > 0){
            throw new CustomException(400,"该采样编号已存在");
        }
    }

    public void editAtlasInfo(Atlas atlas){
        check(atlas.getSampleNumber());
        atlasMapperr.editAtlasInfo(atlas);
    }

    public  void delAtlasInfo(String id) {
        if (StrUtil.hasEmpty(id) || StrUtil.hasBlank(id)) {
            throw new CustomException(400, "缺少信息id");
        }
        String[] strArr = id.split(",");
        Long[] ids = Convert.toLongArray(strArr);
        atlasMapperr.delAtlasInfo(ids);
    }

    public void  addExcel(MultipartFile file, HttpServletRequest request, String uploadPath){
        ExcelReader reader = ExcelUtilHandler.getExcelReader(file, request, uploadPath);
        reader.addHeaderAlias("图谱编号","atlasNumber");
        reader.addHeaderAlias("采样编号","sampleNumber");
        reader.addHeaderAlias("药材名","medicineName");
        reader.addHeaderAlias("基原植物","originalPlant");
        reader.addHeaderAlias("拉丁名","latinName");
        reader.addHeaderAlias("样品来源","sampleSource");
        reader.addHeaderAlias("取样部位","smapleLocation");
        reader.addHeaderAlias("样品提供单位","sampleUnit");
        reader.addHeaderAlias("鉴定人","appraiser");
        reader.addHeaderAlias("责任人","dutyPerson");
        reader.addHeaderAlias("提交时间","sequenceTime");
        reader.addHeaderAlias("图谱类型","atlasType");
        reader.addHeaderAlias("采集信息","collectInfo");
        reader.addHeaderAlias("描述信息","describeInfo");
        List<Atlas> atlasList = reader.readAll( Atlas.class);
        atlasList.stream().forEach(atlas -> {
            atlas.setCreatetime(TimeUtil.getNow());
        });
        atlasMapperr.bachAdd(atlasList);


    }

    public  List<Stat> getStatGroupByType(){
        return atlasMapperr.getStatGroupByType();
    }

    public Integer getTotal(){
        return atlasMapperr.getTotal(null);
    }

    public Integer getYesterdayTotal(){
        String yesterday = TimeUtil.getYesterday();
        return atlasMapperr.getTotal(yesterday);
    }

    public Integer getNowTotal(){
        String now = TimeUtil.getNow();
        return atlasMapperr.getTotal(now);
    }


    public List<Stat> getTotalGroupByDate(String startTime,String endTime){
        return atlasMapperr.getTotalGroupByDate(startTime,endTime);
    }
}
